package com.tedu.adapter.controller;

import com.tedu.adapter.converter.ContentConvertoerBoToVo;
import com.tedu.adapter.protocol.vo.ContentListVO;
import com.tedu.adapter.protocol.vo.ContentVO;
import com.tedu.domain.bo.ContentBO;
import com.tedu.domain.bo.ContentListBO;
import com.tedu.domain.service.ContentService;
import com.tedu.protocol.dto.ContentDTO;
import com.tedu.protocol.param.ContentParam;

import com.tedu.po.Content;
import com.tedu.protocol.param.ContentUpdateParam;
import com.tedu.util.response.AppCode;
import com.tedu.util.response.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * @date 2023/4/19
 * @author liuhuan
 */
@RestController
@Api(tags = "1.内容管理模块")
@RequestMapping("/api/content/")
@Slf4j

public class ContentController {

    @Autowired
    ContentConvertoerBoToVo contentConvertoerBoToVo;
    @Autowired
    ContentService contentService;


    @ApiOperation("新增内容")
    @PostMapping("addNew")
  public ResultVo addNew(@RequestBody ContentParam contentParam) {
        log.info("进入新增内容请求");
        int count= contentService.countByTitle(contentParam.getTitle());
        if(count!=0){
            return new ResultVo(AppCode.FAILED,"标题已存在，请重新起名");
        }
        contentService.addNew(contentParam);
        log.info("请求处理成功");
        return  new ResultVo(AppCode.SUCCESS);
    }

    @ApiOperation("根据ID删除数据")
    @GetMapping("delete")
    public ResultVo deleteById(int id){
        log.info("开始处理根据ID删除数据请求");
        int count= contentService.deleteById(id);
        if(count==0){
            return new ResultVo(AppCode.FAILED,"失败，找不到此内容");
        }
        log.info("删除成功");
        return new ResultVo(AppCode.SUCCESS,"删除成功");
    }

    @ApiOperation("修改内容")
    @PostMapping("update")
    public ResultVo update(ContentUpdateParam contentUpdateParam){
        log.info("开始处理修改内容请求");
        contentService.updateByPrimaryKeySelective(contentUpdateParam);
        log.info("处理请求完成");
        return new ResultVo(AppCode.SUCCESS,"修改完成");
    }


    @ApiOperation("根据标题查重")
    @GetMapping("countByTitle")
    public ResultVo countByTitle(String title){
        log.info("开始处理根据标题查重请求");
        int count= contentService.countByTitle(title);
        if(count!=0){
            return new ResultVo(AppCode.FAILED,"标题已存在，请重新起名");
        }
         return new ResultVo(AppCode.SUCCESS,"成功，无重复");
    }

    @ApiOperation("查询内容详情")
    @GetMapping("info")
    public ResultVo selectById(Integer id){
    log.info("正在处理查询用户信息请求");
    ContentBO contentBO= contentService.selectByPrimaryKey(id);
    if(contentBO==null){
        return new ResultVo(AppCode.FAILED,"对不起，找不到此人");
    }
       ContentVO vo= contentConvertoerBoToVo.boAssembleVO(contentBO);
    return new ResultVo(AppCode.SUCCESS,vo);
    }

    @ApiOperation("查询内容列表")
    @GetMapping("list")
    public ResultVo list(){
        log.info("开始处理查询内容列表请求");
       List<ContentListBO> list= contentService.list();
       List<ContentListVO> list1=contentConvertoerBoToVo.listBoListToVOList(list);
       log.info("查询完成");
       return new ResultVo(AppCode.SUCCESS,list);
    }

}
